package p10.code.exchanger;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Exchanger;

public class Consumer implements Runnable{

    private final Exchanger<List<Integer>> exchanger;

    public Consumer(Exchanger<List<Integer>> exchanger) {
        this.exchanger = exchanger;
    }

    @Override
    public void run() {
        List<Integer> msg = new ArrayList<>();

        while (true) {
            try {
                msg.add(5);
                Thread.sleep(1000);
                msg = exchanger.exchange(msg);
                System.out.println(Thread.currentThread().getName() + ": 消费了数据[" + msg.get(0) + "]");
                msg.remove(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
